<?php

/**
 * test actions.
 *
 * @package    tests
 * @subpackage test
 * @author     Djul
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class testActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeAlltests(sfWebRequest $request)
  {
    $this->results = $this->getUser()->getGuardUser()->getResults();
  }
  
  public function executeShowresult(sfWebRequest $request)
  {

  }
  public function executeDoanswer(sfWebRequest $request)
  {
      if($request->isMethod('post'))
      {
          $this->show_result = false;
          $doanswer = $request->getParameter('Doanswer');
          $this->question = QuestionPeer::retrieveByPK($doanswer['id']);          
          $multiply = $this->question->isMultiply();
          $this->form = new DoanswerForm(array(), array('id' => $doanswer['id'], 'mylti' => $multiply));
          $this->form->bind($request->getParameter($this->form->getName()));
          if($this->getUser()->hasAttribute('result_id'))
          {
             $result = ResultPeer::retrieveByPK($this->getUser()->getAttribute('result_id'));
          }
          else
          {
              $this->redirect('@homepage');
          }
          if($this->form->isValid())
          {              
              //проверка влазит ли ответ в определенное время
              //сделать ответ
              if($result->hasTime())
              {
                  $this->result_answer = $result->getResultAnswerByQuestion($this->form->getValue('id'));
                  $question = $this->result_answer->getQuestion();
                  $ans_arr = array();
                  foreach ($question->getAnswers() as $answer)
                  {
                      if($answer->getTrueAnswer())
                      $ans_arr[] = $answer->getId();
                  }
                  if($multiply)
                  {
                      $post_arr = $this->form->getValue('answer_list');
                  }
                  else
                  {
                      $post_arr = array();
                      $post_arr[] = $this->form->getValue('answer_list');
                  }
                  if($ans_arr == $post_arr)
                  {
                      $this->result_answer->setResult(true);
                  }
                  else
                  {
                      $this->result_answer->setResult(false);

                  }
                  $this->result_answer->save();
                  //build next question
                  if(count($result->getNotAnsweredResultAnswers()) != 0 )
                  {
                      $this->result_answer = $result->getNextResultAnswer($this->result_answer->getId());
                      if($this->result_answer == null)
                      {
                          $this->result_answer = $result->getFirstResultAnswer();
                      }
                      
                      $this->result_answer_next = $result->getNextResultAnswer($this->result_answer->getId());
                      if($this->result_answer_next == null)
                      {
                          $this->result_answer_next = $result->getFirstResultAnswer();
                      }
                      $this->result_answer_prev = $result->getPrevResultAnswer($this->result_answer->getId());
                      if($this->result_answer_prev == null)
                      {
                          $this->result_answer_prev = $result->getLastResultAnswer();
                      }
                      $this->question = $this->result_answer->getQuestion();
                      $multiply = $this->question->isMultiply();
                      $this->form = new DoanswerForm(array(), array('id' => $this->question->getId(), 'mylti' => $multiply));
                      $this->time_to_and = $result->getTimeToAnd();
                  }
                  else
                  {
                      $this->show_result = true;
                  }
              }
              else
              {
                  $this->show_result = true;
              }
          }
          else
          {
             $this->time_to_and = $result->getTimeToAnd();
            $this->result_answer = $result->getResultAnswer($request->getParameter('id'));
            $this->result_answer_next = $result->getNextResultAnswer($request->getParameter('id'));
            if($this->result_answer_next == null)
            {
                $this->result_answer_next = $result->getFirstResultAnswer();
            }
            $this->result_answer_prev = $result->getPrevResultAnswer($request->getParameter('id'));
            if($this->result_answer_prev == null)
            {
                $this->result_answer_prev = $result->getLastResultAnswer();
            }
          }
      }
  }

  public function executeShowquestion(sfWebRequest $request)
  {
    $this->show_result = false;
    if($this->getUser()->hasAttribute('result_id'))
    {
       $result = ResultPeer::retrieveByPK($this->getUser()->getAttribute('result_id'));
    }
    else
    {
        $this->redirect('@homepage');
    }
    if($result->hasTime())
    {
        if($request->hasParameter('id'))
        {
            $this->result_answer = $result->getResultAnswer($request->getParameter('id'));
            $this->result_answer_next = $result->getNextResultAnswer($request->getParameter('id'));            
            if($this->result_answer_next == null)
            {
                $this->result_answer_next = $result->getFirstResultAnswer();
            }
            $this->result_answer_prev = $result->getPrevResultAnswer($request->getParameter('id'));
            if($this->result_answer_prev == null)
            {
                $this->result_answer_prev = $result->getLastResultAnswer();
            }
        }
        else
        {
            $this->result_answer = $result->getFirstResultAnswer();
            $this->result_answer_next = $result->getNextResultAnswer($this->result_answer->getId());
            $this->result_answer_prev = null;
        }
        $this->question = $this->result_answer->getQuestion();
        $multiply = $this->question->isMultiply();
        $this->form = new DoanswerForm(array(), array('id' => $this->question->getId(), 'mylti' => $multiply));
        $this->time_to_and = $result->getTimeToAnd();
    }
    else
    {
        $this->show_result = true;
    }
    
  }
  
  public function executeTest(sfWebRequest $request)
  {
    $this->show_result = false;
    if($this->getUser()->hasAttribute('result_id'))
    {
       $result = ResultPeer::retrieveByPK($this->getUser()->getAttribute('result_id'));
    }
    else
    {
        $this->redirect('@homepage');
    }
    if($result->hasTime() && count($result->getNotAnsweredResultAnswers()) != 0)
    {
        if($request->hasParameter('id'))
        {
            $this->result_answer = $result->getResultAnswer($request->getParameter('id'));
            $this->result_answer_next = $result->getNextResultAnswer($request->getParameter('id'));
            if($this->result_answer_next == null)
            {
                $this->result_answer_next = $result->getFirstResultAnswer();
            }
            $this->result_answer_prev = $result->getPrevResultAnswer($request->getParameter('id'));
            if($this->result_answer_prev == null)
            {
                $this->result_answer_prev = $result->getLastResultAnswer();
            }
        }
        else
        {
            $this->result_answer = $result->getFirstResultAnswer();
            $this->result_answer_next = $result->getNextResultAnswer($this->result_answer->getId());
            $this->result_answer_prev = $result->getLastResultAnswer();
        }
        $this->question = $this->result_answer->getQuestion();
        $multiply = $this->question->isMultiply();
        $this->form = new DoanswerForm(array(), array('id' => $this->question->getId(), 'mylti' => $multiply));
        $this->time_to_and = $result->getTimeToAnd();
    }
    else
    {
        $this->show_result = true;
    }
  }
  
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new TeststartForm();
    if($request->isMethod('post'))
    {
        $form_values = $request->getParameter($this->form->getName());
        $test_questions = TestPeer::buildTestArray($form_values['test']);
        $result = new Result();
        $result->setUserId($this->getUser()->getGuardUser()->getId());
        $result->setTestId($form_values['test']);
        $result->setData(date('Y-m-d H:i:s'));
        $result->save();
        foreach($test_questions as $test_question)
        {
            $result_answers = new ResultAnswers();
            $result_answers->setResultId($result->getId());
            $result_answers->setQuestionId($test_question);
            $result_answers->save();
        }
        $this->getUser()->setAttribute('result_id',$result->getId());
        $this->redirect('@go');
    }
  }
  public function executeTestbox(sfWebRequest $request)
  {
    $this->id = $request->getParameter('id');    
    $this->form = new TeststartForm(array(), array('id' => $this->id));
  }
  public function executeTestbox2(sfWebRequest $request)
  {
    $this->block_id = $request->getParameter('block_id');
    $this->form = new TeststartForm(array(), array('block' => $this->block_id));
  }
}
